package com.yuzhitong.shapi.util;

import android.app.Activity;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.yuzhitong.shapi.BuildConfig;
import a.a.a.MyApplication;
import com.yuzhitong.shapi.base.Contents;

/**
 * Created by Brsya
 * CreateDate: 2022/4/18  14:02
 * Description:
 * 广告工具
 */
public class AdVideoUtilManage {

    // 广告SDK管理
    private TTFullScreenVideoAd showAd;

    private AdVideoUtilManage() {
    }

    private AdVideoUtilManage(TTFullScreenVideoAd showAd) {
        this.showAd = showAd;
    }

    private TTFullScreenVideoAd getShowAd() {
        return showAd;
    }

    private void setShowAd(TTFullScreenVideoAd showAd) {
        this.showAd = showAd;
    }

    public static AdVideoUtilManage initShow(Activity context, boolean nowShow) {
        return initShow(context, nowShow, null);
    }

    public static AdVideoUtilManage initShow(Activity context, boolean nowShow, TTRewardVideoAd.RewardAdInteractionListener listener) {

        if (!MyApplication.isOpenAd()) {
            return null;
        }


        // 加载广告
        TTAdNative adNative = TTAdSdk.getAdManager().createAdNative(context);

        AdSlot adSlot = new AdSlot.Builder()
                // 广告ID
                .setCodeId("948499082")
                .setUserID(BuildConfig.FLAVOR)
                .setAdLoadType(nowShow ? TTAdLoadType.LOAD : TTAdLoadType.PRELOAD)//推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                // 期望模板广告view的size,单位dp
                .setExpressViewAcceptedSize(1, 1)
                .setAdloadSeq(1)
                .build();

        adNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                LoggerUtil.i("穿山甲广告加载失败" + i + "--ss--" + s);
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {

            }

            @Override
            public void onRewardVideoCached() {

            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                if (listener != null) {
                    ttRewardVideoAd.setRewardAdInteractionListener(listener);
                }
                if (nowShow) {
                    ttRewardVideoAd.showRewardVideoAd(context);
                }
            }
        });


        return new AdVideoUtilManage();
    }

    public static AdVideoUtilManage initNewShow(Activity context
            , boolean nowShow
            , TTFullScreenVideoAd.FullScreenVideoAdInteractionListener listener
            , AdLoadListener adLoadListener) {

        if (!MyApplication.isOpenAd()) {
            return null;
        }
        AdVideoUtilManage manage = new AdVideoUtilManage();

        // 加载广告
        TTAdNative adNative = TTAdSdk.getAdManager().createAdNative(context);

        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Contents.AD_TT_SDK_HOME_CLICK_WEB_ID) //广告位id
                .setSupportDeepLink(true)
                .setAdLoadType(nowShow ? TTAdLoadType.LOAD : TTAdLoadType.PRELOAD)//推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build();

        adNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
                    @Override
                    public void onError(int i, String s) {
                        LoggerUtil.i("穿山甲广告加载失败" + i + "--ss--" + s);
                        if (adLoadListener != null) {
                            adLoadListener.error();
                        }
                    }

                    @Override
                    public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ttFullScreenVideoAd) {

                    }

                    @Override
                    public void onFullScreenVideoCached() {

                    }

                    @Override
                    public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {
                        if (adLoadListener != null) {
                            adLoadListener.success();
                        }
                        manage.setShowAd(ttFullScreenVideoAd);
                        ttFullScreenVideoAd.setFullScreenVideoAdInteractionListener(listener);
                        ttFullScreenVideoAd.showFullScreenVideoAd(context, TTAdConstant.RitScenes.GAME_GIFT_BONUS, null);
                    }
                }
        );


        return manage;
    }


    public interface AdLoadListener{
        void success();

        void error();
    }

}
